const labelService = require('../service/label.service')


// 验证添加标签是否已存在的中间件
const verifyLabelExists = async (ctx, next) => {
    const { labels } = ctx.request.body;
    const resultLabels = []
    for (const name of labels) {
        const label = { name }
        const result = await labelService.getLabelByName(name)
        if (result.length) {
            label.id = result[0].id
        } else {
            const res = await labelService.create(name)
            label.id = res.insertId
        }
        resultLabels.push(label)
    }
    ctx.labels = resultLabels
    await next()
}


module.exports = {
    verifyLabelExists,
}